﻿using System;

class BinaryToDecimal
{
    static void Main()
    {
        string binary = "0100111";
        int deciNum = ConvertBinToDeci(binary);

        Console.WriteLine("Binary:  " + binary);
        Console.WriteLine("Decimal: " + deciNum);        
    }

    private static int ConvertBinToDeci(string binStr)
    {
        int deciNum = 0;
        int numBase = 2;
        int len = binStr.Length - 1;

        for (int i = 0; i <= len; i++)
        {
            // Conversion: char -> string -> int
            int digit = Convert.ToInt32(binStr[i].ToString());
            deciNum += digit * (int)Math.Pow(numBase, len - i);
        }

        return deciNum;
    }
}
